/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt.core.ui;

import com.gadglet.client.gwt.GadgetNativeUtils;
import com.gadglet.client.gwt.GadgetUtils;
import com.gadglet.client.gwt.ui.DebugDialogBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Minglet Friends dialog panel
 *
 */
public class FriendsDialogPanel {

	
	DebugDialogBox debug = DebugDialogBox.getErrorNotifier();
	
	private static DialogBox dialogBox = new DialogBox();

	protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();
	
	private final VerticalPanel mainPanel = new VerticalPanel();
	private final VerticalPanel mainContainerPanel = new VerticalPanel();
	private final HorizontalPanel canvasViewPanel = new HorizontalPanel();
	private final ScrollPanel scrollPanel = new ScrollPanel();
	private static FriendsDialogPanel friendsDialogPanel;
	
	final Button closeButton = new Button(prefs.getMsg("gadgetLabelClose"));
	
	public static FriendsDialogPanel getFriendsPanel() {
		if (friendsDialogPanel == null) {
			friendsDialogPanel = new FriendsDialogPanel();
		}
		return friendsDialogPanel;
	}
	
	
	FriendsDialogPanel(){
	
		dialogBox.setText(prefs.getMsg("gadgetLabelFriends"));
		
		final Button closeButton = new Button(prefs.getMsg("gadgetLabelClose"));
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
			
				dialogBox.hide();
			}
		});
		
		int width = GadgetNativeUtils.getGadgetWidth()-100;
		int height = GadgetNativeUtils.getGadgetHeight()-100;
		if (GadgetUtils.isCanvasView())
			height = height/2;
		mainContainerPanel.setSize(width  + "px", height + "px");
		scrollPanel.setPixelSize(width+ 10,height + 10);
				
		
		if(GadgetUtils.isCanvasView()){
		
			canvasViewPanel.add(LocalFriendsPanel.getFriendsPanel().getLocalFrindsContainer());
			canvasViewPanel.setCellWidth(LocalFriendsPanel.getFriendsPanel().getLocalFrindsContainer(),width/3+"px" );
			canvasViewPanel.add(RemoteFriendsPanel.getRemoteFriendsPanel().getRemoteFrindsContainer());
			canvasViewPanel.setCellWidth(RemoteFriendsPanel.getRemoteFriendsPanel().getRemoteFrindsContainer(),width/3+"px" );
			canvasViewPanel.add(InvitationsPanel.getInvitationsPanel().getInvitationsContainer());
			canvasViewPanel.setCellWidth(InvitationsPanel.getInvitationsPanel().getInvitationsContainer(),width/3+"px" );

			mainContainerPanel.add(canvasViewPanel);
		}
		else
		{
			mainContainerPanel.add(LocalFriendsPanel.getFriendsPanel().getLocalFrindsContainer());
			mainContainerPanel.add(RemoteFriendsPanel.getRemoteFriendsPanel().getRemoteFrindsContainer());
			mainContainerPanel.add(InvitationsPanel.getInvitationsPanel().getInvitationsContainer());
		}
		scrollPanel.add(mainContainerPanel);
		
		mainPanel.add(scrollPanel);
		
		mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		mainPanel.add(closeButton);
		
		dialogBox.setWidget(mainPanel);
		
	}
	
	public  void showDialog(){
		
		LocalFriendsPanel.getFriendsPanel().viewLocalFriends();
		RemoteFriendsPanel.getRemoteFriendsPanel().viewRemoteFriends();
		InvitationsPanel.getInvitationsPanel().viewInvitations();
		
		if (GadgetUtils.isCanvasView())
			GadgetUtils.dialogBoxTop(dialogBox);
		else
			dialogBox.center();
		
	}
	
	
}
